import * as dayjs from "dayjs";
// {
//     year: 2022,
//     data: [
//       {
//         month: 1,
//         data: [
//           {
//             day: 1,
//             data: [
//               { type: "warning", content: "吃饭" },
//               { type: "success", content: "过生日" },
//             ],
//           },
//         ],
//       },
//     ],
//   },

export interface iThingsData {
  data: {
    type: string;
    content: string;
    time: string;
  }[];
}

export default class CalendarUtil {
  public thingsData: iThingsData;
  public handleThingsData: any = {};
  constructor(thingsData: iThingsData) {
    this.thingsData = thingsData;
    this.handleThings();
  }
  public handleThings(): any {
    this.handleThingsData = this.thingsData.data.reduce(
      (pre: any, item: { type: string; content: string; time: string }) => {
        // debugger
        let itemTime = dayjs(item.time);
        let year = itemTime.year();
        let month = itemTime.month() + 1;
        let day = itemTime.date();
        if (Object.prototype.hasOwnProperty.call(pre, year)) {
          // 如果有年
          if (Object.prototype.hasOwnProperty.call(pre[year], month)) {
            // 如果有月
            if (Object.prototype.hasOwnProperty.call(pre[year][month], day)) {
              // 如果有日
              pre[year][month][day].data.push({
                type: item.type,
                content: item.content,
                time: item.time,
              });
            } else {
              // 如果没有日
              pre[year][month][day] = {
                data: [
                  {
                    type: item.type,
                    content: item.content,
                    time: item.time,
                  },
                ],
              };
            }
          } else {
            // 如果没有月
            pre[year][month] = {
              [day]: {
                data: [
                  {
                    type: item.type,
                    content: item.content,
                    time: item.time,
                  },
                ],
              },
            };
          }
        } else {
          // 如果没有年
          pre[year] = {
            [month]: {
              [day]: {
                data: [
                  {
                    type: item.type,
                    content: item.content,
                    time: item.time,
                  },
                ],
              },
            },
          };
        }
        return pre;
      },
      {}
    );
    return this.handleThingsData;
  }
  public getHandleThingsData() {
    return this.handleThingsData;
  }
}

export let getDataFromYearMonthDay = (
  json: any,
  year: string,
  month: string,
  day: string
) => {
  if (Object.prototype.hasOwnProperty.call(json, year)) {
    // 如果有年
    if (Object.prototype.hasOwnProperty.call(json[year], month)) {
      // 如果有月
      if (Object.prototype.hasOwnProperty.call(json[year][month], day)) {
        // 如果有日
        return json[year][month][day].data;
      } else {
        return [];
      }
    } else {
      return [];
    }
  } else {
    return [];
  }
};
